﻿using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class BaseDAL<T> : IBaseDAL<T> where T : class
    {
        public BaseDAL(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }
        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="t">实体对象</param>
        /// <returns></returns>
        public async Task<int> AddAsync(T t)
        {
           Context.Set<T>().Add(t);
            return await Context.SaveChangesAsync();
        }
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param int="Id">编号id</param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(int Id)
        {
            var obj = Context.Set<T>().Find(Id);
            Context.Set<T>().Remove(obj);
            return await Context.SaveChangesAsync();

        }
        /// <summary>
        /// 根据编号查询一条信息
        /// </summary>
        /// <param int="Id">编号id</param>
        /// <returns></returns>
        public IQueryable<T> GetAll()
        {
            return Context.Set<T>();
        }
        /// <summary>
        /// 预备查
        /// </summary>
        /// <param name="Id">编号id</param>
        /// <returns></returns>
        public T GetValue(int Id)
        {
            return Context.Set<T>().Find(Id);
        }
        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="t">实体对象</param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T t)
        {
           Context.Set<T>().Update(t);
            return await Context.SaveChangesAsync();
        }
    }
}
